package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetSowingRecordTable(ctx *context.Context) table.Table {

	sowingRecord := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := sowingRecord.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("订单", "name", db.Varchar).
		FieldJoin(types.Join{
			Table:     "orders",
			JoinField: "id",
			Field:     "order_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("orders", "name", "name", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 2)
		}).
		FieldSortable()
	info.AddField("播种日期", "sowing_date", db.Date).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("播种数量", "quantity", db.Int).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()

	info.AddField("播种管理员", "username", db.Varchar).
		FieldJoin(types.Join{
			Table:     "goadmin_users",
			JoinField: "id",
			Field:     "manager_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("goadmin_users", "username", "username", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 2)
		}).
		FieldSortable()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("修改时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("sowing_record").SetTitle("播种记录").SetDescription("SowingRecord")

	formList := sowingRecord.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldDisableWhenUpdate()
	formList.AddField("订单", "order_id", db.Int, form.SelectSingle).FieldOptionsFromTable("orders", "name", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 0)
	}).FieldHelpMsg("请选择订单")
	formList.AddField("播种日期", "sowing_date", db.Date, form.Date)
	formList.AddField("播种数量", "quantity", db.Int, form.Number)
	formList.AddField("管理员", "manager_id", db.Int, form.SelectSingle).FieldOptionsFromTable("goadmin_users", "username", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 2)
	}).FieldHelpMsg("请选择播种管理员")
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("sowing_record").SetTitle("播种记录").SetDescription("SowingRecord")

	return sowingRecord
}
